<?php
require_once 'enums.php';

final class input extends field
{
  public $type = InputType::TEXT;  
  
  public function display()
  {
    print '<p>';
    
    print $this->printLabel();
    
    print '<input' . $this->printClass() . $this->printId() . $this->printName() . $this->printType() . $this->printValue() . $this->printOnChange() . ' />';    
    
    print '</p>';
  }  
  
  private function printType()
  {
    return ' type="'.$this->type.'" ';
  }
  
  private function printValue()
  {
    return ' value="'.$this->value.'" ';
  }  
}  


final class choice extends field
{
  public $multi = false;
   
  public $options = array();
  
  private $type = InputType::RADIO;
   
  public function display()
  {    
    print '<p>';
    
    print $this->printLabel();    
    
    foreach ($this->options as $optionLabel => $option)
    {
      print '<br /><label>'.__($optionLabel).'</label>';
      
      print '<input'.$this->printType() . $this->printId($this->id . '-' . $option->value). $this->printName() . $this->printValue($option->value) . $this->printSelected($option->value). $this->printOnChange($option->onChange) . ' >';     
    }
    
    print '</p>';    
    
  }  
  
  private function printType()
  {
    return ' type="'.$this->type.'" ';
  }
  
  private function printValue($optionValue)
  {
    return ' value="'.$optionValue.'" ';
  }    
  
  private function printSelected($optionValue)
  {
    if($this->value == $optionValue)
    {
      return 'CHECKED';
    }
  }
}

abstract class field
{
  public $onChange = null;
  
  protected $name = null;
  
  protected $id = null;
  
  public $label = null;
  
  public $class = null;  
  
  public $value = null;  
  
  public $style;
  
  public function __construct($name, $id)
  {
    $this->id = $id;
    $this->name = $name;
  }
  
  protected function printClass()
  {
    if($this->class != null)
    {
      return ' class="'.$this->class.'" ';      
    }
    
    return null;
  }
  
  protected function printId($id = null)
  {
    if($id)
    {
      $_id = $id;
    }
    else
    {
      $_id = $this->id;
    }
    
    return ' id="'.$_id.'" ';          
  }
  
  protected function printName()
  {
    return ' name="'.$this->name.'" ';
  }
  
  protected function printLabel()
  {
    if($this->label != null)
    {
      return  '<label for="' .$this->id. '">' . __($this->label . ':') . '</label>';
    }  
    
    return null;
  }
  
  protected function printOnChange($onChange = null)
  {   
    if($onChange != null)
    {
      $_onChange = $onChange;
    }
    else if($this->onChange != null)
    {
      $_onChange = $this->onChange;
    }
    if($_onChange != null)
    {

      
      return ' onchange='.$_onChange;      
    }
    
    return null;    
  }  
  
}

class option extends field
{

  public function __construct($value, $onChange = null)
  {
    $this->value = $value;
    
    $this->onChange = $onChange;

  }

}
  
?>